//
//  SXHTTPManager.m
//
//  Created by 孙晓奇 on 15/8/10.
//  Copyright (c) 2015年 sunny. All rights reserved.
//

#import "HTTPManager.h"
#import "FilterModel.h"
#import "LocationModel.h"
#import "UserModel.h"

@implementation HTTPManager

+ (instancetype)manager
{
    HTTPManager *manager = [super manager];
    
    manager.requestSerializer.timeoutInterval = 30.0f;   // 网络超时30s
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",
                                                         @"text/javascript", @"text/html", @"text/plain",
                                                         @"application/x-www-form-urlencoded",nil];
    manager.securityPolicy.allowInvalidCertificates = YES;   // 支持没有第三方信任签名证书的https请求
    return manager;
}

// login
-(void)login:(UserModel *)user
     Success:(void (^)(NSDictionary *responseObject)) sucess
     failure:(void (^)(NSError *error)) failure
{
    NSString *url = [NSString stringWithFormat:@"%@/login", HOST];
    NSDictionary *parameters = @{
                                 @"username":user.userName? user.userName: @"",
                                 @"password":user.password? user.password: @""
                                 };
    [self POST:url parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        sucess(responseObject);
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        failure(error);
    }];
}

// get city list
-(void)getCityListWithSuccess:(void (^)(NSArray *responseObject)) sucess
                      failure:(void (^)(NSError *error)) failure
{
    NSString *url = [NSString stringWithFormat:@"%@/locations", HOST];
    [self GET:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        sucess(responseObject);
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        failure(error);
    }];
}

// get office rooms
//api/locations/{cityid}/rooms?cMin={capacity min}&cMax={capacity max}&equipments=VC,Screen
-(void)getOfficeRoomList:(FilterModel *)filter
                 success:(void (^)(NSArray *responseObject)) sucess
                 failure:(void (^)(NSError *error)) failure
{
    NSDictionary *parameters;
    if (filter.equipStr == nil || [filter.equipStr isEqualToString:@""]) {
        parameters = nil;
    } else {
        parameters = @{
                       @"cMin":[NSString stringWithFormat:@"%li", filter.capMin],
                       @"cMax":[NSString stringWithFormat:@"%li", filter.capMax],
                       @"equipments":filter.equipStr? filter.equipStr: @"",
                     };
    }
    NSString *url = [NSString stringWithFormat:@"%@/locations/%i/rooms", HOST, filter.location.id];
    [self GET:url parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        sucess(responseObject);
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        failure(error);
    }];
}


-(void)getBookingListWithStartTime:(NSDate *)startTime
                           endTime:(NSDate *)endTime
                           success:(void (^)(NSArray *responseObject)) sucess
                           failure:(void (^)(NSError *error)) failure
{
    NSString *userEmail = [UserDefaultDataHelper getDataWithKey:UserDefaultKeyUserEmail];
    if (userEmail == nil || [userEmail isEqualToString:@""]) {
        NSLog(@"尚未登陆, 请先登陆");
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@/users/%@/reservations", HOST, userEmail];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-MM-dd";
    NSDictionary *parameters = @{
                                 @"starttime":[formatter stringFromDate:startTime]? [formatter stringFromDate:startTime]: @"",
                                 @"endtime":[formatter stringFromDate:endTime]? [formatter stringFromDate:endTime]: @"",
                                };
    //NSLog(@"%@", parameters);
    [self GET:url parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        sucess(responseObject);
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        failure(error);
    }];
}
@end
